package com.cskaoyan.service.admin.promotion;

import com.cskaoyan.bean.MarketAd;
import com.cskaoyan.bean.MarketGoods;
import com.cskaoyan.bean.MarketTopic;
import com.cskaoyan.bean.MarketTopicExample;
import com.cskaoyan.bean.common.BasePageInfo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.bean.vo.admin.promotion.AdminTopicAndGoodsVO;
import com.cskaoyan.bean.vo.admin.promotion.AdminTopicVO;
import com.cskaoyan.mapper.MarketGoodsMapper;
import com.cskaoyan.mapper.MarketTopicMapper;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/*
 * @Auther:王贲
 * @Date:2022/9/7
 * @Description:
 * @VERSON:1.8
 */
@Service
@Transactional
public class AdminTopicServiceImpl implements AdminTopicService {

    @Autowired
    MarketTopicMapper marketTopicMapper;

    @Autowired
    MarketGoodsMapper marketGoodsMapper;

    /**
     * @description : TODO 专题页面的显示与模糊查询的实现
     * @return com.cskaoyan.bean.common.CommonData
     * @author wang'ben
     * @date 2022/9/7 21:04
     */
    @Override
    public CommonData selectTopicInfo(BasePageInfo basePageInfo, String title, String subtitle) {
        PageHelper.startPage(basePageInfo.getPage(), basePageInfo.getLimit());
        MarketTopicExample marketTopicExample = new MarketTopicExample();
        MarketTopicExample.Criteria criteria = marketTopicExample.createCriteria();
        marketTopicExample.setOrderByClause(basePageInfo.getSort() + " " + basePageInfo.getOrder());
        if (!StringUtils.isEmpty(title)) {
            criteria.andTitleLike("%" + title + "%");
        }
        if (!StringUtils.isEmpty(subtitle)) {
            criteria.andSubtitleLike("%" + subtitle + "%");
        }
        List<MarketTopic> list = marketTopicMapper.selectByExample(marketTopicExample);
        ArrayList<MarketTopic> topics = new ArrayList<>();

        for (MarketTopic marketTopic : list) {
            if (marketTopic.getDeleted()) {
                continue;
            }
            topics.add(marketTopic);
        }

        long total = marketTopicMapper.countByExample(null);
        CommonData commonData = new CommonData<>();
        commonData.setTotal((int)total);
        commonData.setPages(basePageInfo.getPage());
        commonData.setLimit(basePageInfo.getLimit());
        commonData.setPage(basePageInfo.getPage());
        commonData.setList(topics);
        return commonData;
    }

    /**
     * @description : TODO 添加专题方法的实现
     * @return void
     * @author wang'ben
     * @date 2022/9/7 21:09
     */
    @Override
    public void insertTopicInfo(MarketTopic marketTopic) {
        marketTopic.setAddTime(new Date());
        marketTopic.setUpdateTime(new Date());
        marketTopicMapper.insertSelective(marketTopic);
    }

    /**
     * @description : TODO 专题删除方法的实现
     * @return void
     * @author wang'ben
     * @date 2022/9/7 23:04
     */
    @Override
    public void deleteTopicInfo(MarketTopic marketTopic) {
        marketTopic.setDeleted(true);
        marketTopicMapper.updateByPrimaryKeySelective(marketTopic);
    }

    @Override
    public void batcheDeleteInfo(AdminTopicVO adminTopicVO) {
        Integer[] ids = adminTopicVO.getIds();
        MarketTopic marketTopic ;
        for (Integer id : ids) {
            marketTopic = marketTopicMapper.selectByPrimaryKey(id);
            marketTopic.setDeleted(true);
            marketTopicMapper.updateByPrimaryKeySelective(marketTopic);
        }


    }

    @Override
    public AdminTopicAndGoodsVO updateTopicInfo(Integer id) {
        MarketTopic topic = marketTopicMapper.selectByPrimaryKey(id);
        List<MarketGoods> goodsList = marketGoodsMapper.selectByExample(null);
        AdminTopicAndGoodsVO adminTopicAndGoodsVO = new AdminTopicAndGoodsVO();
        adminTopicAndGoodsVO.setTopic(topic);

        adminTopicAndGoodsVO.setGoodsList(goodsList);
        return adminTopicAndGoodsVO;
    }

    @Override
    public void updateTopic(MarketTopic marketTopic) {
        marketTopicMapper.updateByPrimaryKeySelective(marketTopic);
    }


}
